package com.drools.demo;

import com.drools.demo.domain.Person;
import com.drools.demo.service.DemoService;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lkz
 * @Title: TestDemoController
 * @Description: TODO
 * @Date: 2022/12/13 10:27
 */
@RestController
public class TestDemoController {

    @Resource
    private DemoService demoService;

    @GetMapping("one")
    public void test(){

        KieServices kieServices = KieServices.Factory.get();
        // 获取Kie容器对象（默认容器对象
        KieContainer kieContainer = kieServices.newKieClasspathContainer();
        // 从Kie容器对象中获取会话对象（默认session对象
        KieSession kieSession = kieContainer.newKieSession();
        kieSession.setGlobal("demoService",demoService);
        Person fact = new Person();
        String names = "王小白";
        fact.setNames(names);

        List<String> list = new ArrayList<String>();
        list.add("小黑");
        list.add("王小白");
        list.add(names);
        fact.setList(list);
        // 将order对象插入工作内存
        kieSession.insert(fact);

        // 匹配对象
        // 激活规则，由drools框架自动进行规则匹配。若匹配成功，则执行
        kieSession.fireAllRules();

        // 关闭会话
        kieSession.dispose();



    }
}
